package com.fhminyi.GUI;

import com.fhminyi.dao.ProductDao;
import com.fhminyi.model.Product;
import com.fhminyi.model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

public class DeleteProductFrame extends JFrame {
    private User currentUser;
    private JComboBox<ProductWrapper> productComboBox; // 修改: 将泛型类型改为 ProductWrapper
    private JButton deleteButton;

    public DeleteProductFrame(User currentUser) {
        this.currentUser = currentUser;
        setTitle("删除商品");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 1));

        JLabel label = new JLabel("选择要删除的商品:");
        panel.add(label);

        productComboBox = new JComboBox<>();
        loadProducts();
        panel.add(productComboBox);

        deleteButton = new JButton("删除");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ProductWrapper selectedWrapper = (ProductWrapper) productComboBox.getSelectedItem(); // 修改: 直接获取 ProductWrapper
                if (selectedWrapper != null) {
                    Product selectedProduct = selectedWrapper.getProduct();
                    int selectedOption = JOptionPane.showConfirmDialog(DeleteProductFrame.this, "确定要删除商品吗？", "确认删除", JOptionPane.YES_NO_OPTION);
                    if (selectedOption == JOptionPane.YES_OPTION) {
                        try {
                            ProductDao productDao = new ProductDao();
                            productDao.deleteProduct(selectedProduct.getProductId());
                            JOptionPane.showMessageDialog(DeleteProductFrame.this, "商品删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                            loadProducts();
                        } catch (SQLException ex) {
                            JOptionPane.showMessageDialog(DeleteProductFrame.this, "删除商品失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
        panel.add(deleteButton);

        add(panel);
    }

    private void loadProducts() {
        productComboBox.removeAllItems();
        try {
            ProductDao productDao = new ProductDao();
            List<Product> products = productDao.getProductsByUserId(currentUser.getUserId());
            for (Product product : products) {
                // 修改: 将商品的名称、数量和价格组合成一个字符串显示在 JComboBox 中
                String productInfo = product.getProductName() + " - 数量: " + product.getStock() + " - 价格: " + product.getPrice();
                productComboBox.addItem(new ProductWrapper(product, productInfo));
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "获取商品列表失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    // 添加: 创建一个内部类来包装 Product 对象和显示信息
    private class ProductWrapper {
        private Product product;
        private String displayText;

        public ProductWrapper(Product product, String displayText) {
            this.product = product;
            this.displayText = displayText;
        }

        public Product getProduct() {
            return product;
        }

        @Override
        public String toString() {
            return displayText;
        }
    }
}